// 条件变量的封装

#pragma once

#include <iostream>
#include <pthread.h>
#include "Mutex.hpp"

using namespace MutexModule;

namespace CondModule
{
    class Cond
    {
    public:
        Cond()
        {
            pthread_cond_init(&_cond, nullptr);
        }

        void Wait(Mutex &mutex)
        {
            int n = pthread_cond_wait(&_cond, mutex.Get());
            if (n != 0)
            {
                std::cout << "pthread_cond_wait error: " << n << std::endl;
            }
        }

        void Signal()
        {
            int n = pthread_cond_signal(&_cond);
            if (n != 0)
            {
                std::cout << "pthread_cond_signal error: " << n << std::endl;
            }
        }

        void Broadcast()
        {
            int n = pthread_cond_broadcast(&_cond);
            if (n != 0)
            {
                std::cout << "pthread_cond_broadcast error: " << n << std::endl;
            }
        }

        ~Cond()
        {
            pthread_cond_destroy(&_cond);
        }


    private:
        pthread_cond_t _cond;
    };
}